//
//  SNKTabBarControllerConfig.m
//  SleepNews
//
//  Created by Kavee DJ on 2017/1/10.
//  Copyright © 2017年 Kavee DJ. All rights reserved.
//

#import "SNKTabBarControllerConfig.h"

#import "SNKNavigationController.h"

#import "SNKNewsPageController.h"
#import "SNKPhotoViewController.h"
#import "SNKVideoPageController.h"
#import "SNKMeViewController.h"

@interface SNKTabBarControllerConfig ()

@property (nonatomic, readwrite, strong) CYLTabBarController *tabBarController;

@end

@implementation SNKTabBarControllerConfig

/**
 *  lazy load tabBarController
 *
 *  @return CYLTabBarController
 */
- (CYLTabBarController *)tabBarController {
    if (_tabBarController == nil) {
        CYLTabBarController *tabBarController = [CYLTabBarController tabBarControllerWithViewControllers:self.viewControllers
                                                                                   tabBarItemsAttributes:self.tabBarItemsAttributesForController];
        [self customizeTabBarAppearance:tabBarController];
        _tabBarController = tabBarController;
    }
    return _tabBarController;
}

- (NSArray *)viewControllers {
    SNKNewsPageController *newsVC = [[SNKNewsPageController alloc] init];
    SNKNavigationController *nav1 = [[SNKNavigationController alloc] initWithRootViewController:newsVC];
    
    SNKPhotoViewController *photoVC = [[SNKPhotoViewController alloc] init];
    SNKNavigationController *nav2 = [[SNKNavigationController alloc] initWithRootViewController:photoVC];
    
    SNKVideoPageController *videoVC = [[SNKVideoPageController alloc] init];
    SNKNavigationController *nav3 = [[SNKNavigationController alloc] initWithRootViewController:videoVC];
    
    SNKMeViewController *meVC = [[SNKMeViewController alloc] init];
    SNKNavigationController *nav4 = [[SNKNavigationController alloc] initWithRootViewController:meVC];

    NSArray *viewControllers = @[
                                 nav1,
                                 nav2,
                                 nav3,
                                 nav4,
                                 ];
    return viewControllers;
}

- (NSArray *)tabBarItemsAttributesForController {
    NSDictionary *tab1 = @{
                                                   CYLTabBarItemTitle : @"新闻",
                                                   CYLTabBarItemImage : @"tabbar_news",  /* NSString and UIImage are supported*/
                                                   CYLTabBarItemSelectedImage : @"tabbar_news_hl", /* NSString and UIImage are supported*/
                                                   };
    NSDictionary *tab2 = @{
                                                   CYLTabBarItemTitle : @"图片",
                                                   CYLTabBarItemImage : @"tabbar_picture",
                                                   CYLTabBarItemSelectedImage : @"tabbar_picture_hl",
                                                   };
    NSDictionary *tab3 = @{
                                                   CYLTabBarItemTitle : @"视频",
                                                   CYLTabBarItemImage : @"tabbar_video",
                                                   CYLTabBarItemSelectedImage : @"tabbar_video_hl",
                                                   };
    NSDictionary *tab4 = @{
                                                   CYLTabBarItemTitle : @"我的",
                                                   CYLTabBarItemImage : @"tabbar_setting",
                                                   CYLTabBarItemSelectedImage : @"tabbar_setting_hl",
                                                   };
    
    NSArray *tabBarItemsAttributes = @[
                                       tab1,
                                       tab2,
                                       tab3,
                                       tab4,
                                       ];
    return tabBarItemsAttributes;
}

/**
 *  更多TabBar自定义设置：比如：tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性等等
 */
- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
#warning CUSTOMIZE YOUR TABBAR APPEARANCE
    // Customize UITabBar height
    // 自定义 TabBar 高度
    tabBarController.tabBarHeight = 40.f;
    
    // set the text color for unselected state
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    // set the text color for selected state
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:219/255.0f green:86/255.0f blue:85/255.0f alpha:1];
    
    // set the text Attributes
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    // Set the dark color to selected tab (the dimmed background)
    // TabBarItem选中后的背景颜色
    // [self customizeTabBarSelectionIndicatorImage];
    
    // update TabBar when TabBarItem width did update
    // If your app need support UIDeviceOrientationLandscapeLeft or UIDeviceOrientationLandscapeRight，
    // remove the comment '//'
    // 如果你的App需要支持横竖屏，请使用该方法移除注释 '//'
    // [self updateTabBarCustomizationWhenTabBarItemWidthDidUpdate];
    
    // set the bar shadow image
    // This shadow image attribute is ignored if the tab bar does not also have a custom background image.So at least set somthing.
//    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
//    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
//    [[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"tapbar_top_line"]];
    
    // set the bar background image
    // 设置背景图片
    // UITabBar *tabBarAppearance = [UITabBar appearance];
    // [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbar_background"]];
    
    // remove the bar system shadow image
    // 去除 TabBar 自带的顶部阴影
//     [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
}

@end
